import { replace } from "lodash-es";

type cfg = {
	children?: string;
	label?: string;
	value?: string;
}
export const recursionFilter = (s, cb, cfg:cfg) => {
	let opt = Object.assign({children: 'children', label: 'label', value: 'value'}, cfg)
	const CHILDREN_KEY = opt.children;
	const LABLE_KEY = opt.label;
	const VALUE_KEY = opt.value;
	function filterCus(s) {
		return s.map(e => ({...e})).filter(e => {
					LABLE_KEY && (e.label = e[LABLE_KEY]);
					VALUE_KEY && (e.value = e[VALUE_KEY]);
					e[CHILDREN_KEY] = e[CHILDREN_KEY] && filterCus(e[CHILDREN_KEY]);
					return cb(e);
				})
	}
	return filterCus(s);
}

export const replaceFields = (s, cfg: Record<string, string>) => {
	if(!cfg) {
		throw new Error('请输入cfg参数')
		return;
	};
	let fieldsArray = Object.entries(cfg);
	function filterCus(s) {
		return s.map(e => ({...e})).map(e => {
					fieldsArray.forEach(f => {
						e[f[1]] = e[f[0]]
						delete e[f[0]]
					})
					e.children = e.children && filterCus(e.children)
					return e;
				})
	}
	return filterCus(s);
}